package org.example.ecr;

/**
 * @author: scout.du
 * @date: 2023/11/15 10:43
 **/

import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sts.StsClient;
import software.amazon.awssdk.services.sts.model.GetCallerIdentityResponse;

public class GetAccountIdExample {

    public static void main(String[] args) {
        // 创建 STS 客户端
        StsClient stsClient = StsClient.builder()
                .region(Region.AWS_GLOBAL) // STS 服务的区域是全球的
                .credentialsProvider(ProfileCredentialsProvider.builder().profileName("scout").build())
                .build();

        // 获取当前 AWS 账户 ID
        GetCallerIdentityResponse response = stsClient.getCallerIdentity();

        // 打印账户 ID
        String accountId = response.account();
        System.out.println("AWS Account ID: " + accountId);

        // 关闭 STS 客户端
        stsClient.close();
    }
}

